<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Message;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class MessageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Message(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->disableCreateButton();
            // $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('message-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('message-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('message-delete')) {
                $grid->disableDeleteButton();
            }

            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('phone');
            $grid->column('email');
            $grid->column('content')->limit(10);
            if (Admin::user()->can('message-status')) {
                $grid->column('status')->select([0 => '待处理', 1 => '已处理', 2 => '驳回']);
            }

            $grid->column('updated_at')->sortable();
            $grid->model()->orderBy('id', 'desc');

            $grid->quickSearch(['name', 'phone', 'email'])->placeholder('请输入姓名、手机号、邮箱');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Message(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('phone');
            $show->field('email');
            $show->field('content');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Message(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('phone');
            $form->text('email');
            $form->text('content');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
